﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAO;
using System.Data.SqlClient;
using System.Data;
using System.Data.Linq;
using DTO;

namespace BUS
{
    public class BUS_SinhVien : IDisposable
    {
        DataClasses1DataContext dbs = new DataClasses1DataContext();

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
                if (dbs != null)
                {
                    dbs.Dispose();
                    dbs = null;
                }
        }

        public List<SinhVien_DTO> LoadDSSinhVien()
        {
            var query =
                (from sv in dbs.SinhViens
                 join khoa in dbs.Khoas on sv.MaKhoa equals khoa.MaKhoa
                 select new SinhVien_DTO
                 {
                     MaSV = sv.MaSV,
                     HoTen = sv.HoTen,
                     NgSinh = sv.NgSinh,
                     NoiSinh = sv.NoiSinh,
                     GioiTinh = sv.GioiTinh,
                     DanToc = sv.DanToc,
                     SoCMND = sv.SoCMND,
                     LoaiSV = sv.LoaiSV,
                     TinhTrang = sv.TinhTrang,
                     MaKhoa = sv.Khoa.TenKhoa,
                     DiaChi = sv.DiaChi,
                     DienThoai = sv.DienThoai,
                     Email = sv.Email,
                     GhiChu = sv.GhiChu
                 }).ToList<SinhVien_DTO>();
            return query;
        }

        public List<SinhVien_DTO> layThongTinSV(string mssv)
        {
            var query =
                (from sv in dbs.SinhViens
                 join khoa in dbs.Khoas on sv.MaKhoa equals khoa.MaKhoa
                 where sv.MaSV == mssv
                 select new SinhVien_DTO
                 {
                     MaSV = sv.MaSV,
                     HoTen = sv.HoTen,
                     NgSinh = sv.NgSinh,
                     NoiSinh = sv.NoiSinh,
                     GioiTinh = sv.GioiTinh,
                     DanToc = sv.DanToc,
                     SoCMND = sv.SoCMND,
                     LoaiSV = sv.LoaiSV,
                     TinhTrang = sv.TinhTrang,
                     MaKhoa = sv.Khoa.TenKhoa,
                     DiaChi = sv.DiaChi,
                     DienThoai = sv.DienThoai,
                     Email = sv.Email,
                     GhiChu = sv.GhiChu
                 }).ToList<SinhVien_DTO>();
            return query;
        }

        public void ThemSinhVien()
        {
            SinhVien sv = new SinhVien()
            {
                // MaSV = int.Parse(MaSV)
            };

            dbs.SinhViens.InsertOnSubmit(sv);

            dbs.SubmitChanges();

        }
    }
}
